package cn.hfatec.shms.web.cw.cwHb.action;

import cn.hfatec.shms.web.xc.xcTjmxb.model.XcTjmxbParams;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.Arrays;
import javax.annotation.Resource;


import cn.hfatec.JsonResponse;
import cn.hfatec.Page;
import cn.hfatec.Table;
import cn.hfatec.constants.SysContant;
import cn.hfatec.freemarker.FreeMarkerUtil;
import cn.hfatec.shms.web.cw.cwHb.model.CwHbParams;
import cn.hfatec.shms.web.cw.cwHb.model.CwHb;
import cn.hfatec.shms.web.cw.cwHb.service.CwHbService;
import cn.hfatec.shms.base.ShmsBaseAction;
import org.springframework.ui.Model;

import javax.validation.Valid;

import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;


/**
 * @author hfatec codegen
 * @version 1.0
 *          <p/>
 *          the JAVA code is generate by middlegen
 */
@Controller
@RequestMapping("/cw/cwHb")
public class CwHbAction extends ShmsBaseAction {

    //服务层
    @Resource(name = "CwHbService")
    private CwHbService cwHbService;


    /**
     * 查询列表
     */
    @RequestMapping("/list")
    public String list(CwHbParams cwHbParams, Page page, Model model) {
        page.setPageSize(100);
        Table table = cwHbService.queryCwHbTable(cwHbParams, page);
        model.addAttribute("cwHbsTable", table);
        model.addAttribute("cwHbParams", cwHbParams);
        return "/cw/cwHb/list";
    }

    /**
     * 异步查询列表
     */
    @RequestMapping("/ajaxList")
    @ResponseBody
    public String ajaxList(CwHbParams cwHbParams, Page page, Model model) {
        try {
            list(cwHbParams, page, model);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/cw/cwHb/table", model)).toString();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     * 查询列表
     */
    @RequestMapping("/rJlist")
    public String rJlist(CwHbParams cwHbParams, Page page, Model model) {
        page.setPageSize(100);
        Table table = cwHbService.queryCwHbTableRj(cwHbParams, page);
        model.addAttribute("cwHbsTable", table);
        model.addAttribute("cwHbParams", cwHbParams);
        return "/cw/cwHb/rJlist";
    }

    /**
     * 异步查询列表
     */
    @RequestMapping("/rJajaxList")
    @ResponseBody
    public String rJajaxList(CwHbParams cwHbParams, Page page, Model model) {
        try {
            rJlist(cwHbParams, page, model);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/cw/cwHb/rJtable", model)).toString();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }


    /**
     * 查询列表
     */
    @RequestMapping("/rJMxlist")
    public String rJMxlist(CwHbParams cwHbParams, Page page, Model model) {
        page.setPageSize(100);
        Table table = cwHbService.queryCwHbTableRjMx(cwHbParams, page);
        model.addAttribute("cwHbsTable", table);
        model.addAttribute("cwHbParams", cwHbParams);
        return "/cw/cwHb/rJlist";
    }
    /**
     * 跳转到选择产品权益页
     */
    @RequestMapping("/doRjMx")
    @ResponseBody
    public String doRjMx(CwHbParams cwHbParams, Page page, Model model) {

        try {
            rJMxlist(cwHbParams, page, model);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/cw/cwHb/selectRjMxb", model)).toString();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /*
         * 异步查询列表
         */
    @RequestMapping("/ajaxSelectList")
    @ResponseBody
    public String ajaxSelectList(CwHbParams cwHbParams, Model model, Page page) {
        try {
            rJMxlist(cwHbParams, page, model);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/cw/cwHb/selectTable", model)).toString();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }


    /**
     * 查询列表
     */
    @RequestMapping("/fYlist")
    public String fYlist(CwHbParams cwHbParams, Page page, Model model) {
        page.setPageSize(100);
        Table table = cwHbService.queryCwHbTableFy(cwHbParams, page);
        model.addAttribute("cwHbsTable", table);
        model.addAttribute("cwHbParams", cwHbParams);
        return "/cw/cwHb/fYlist";
    }

    /**
     * 异步查询列表
     */
    @RequestMapping("/fYajaxList")
    @ResponseBody
    public String fYajaxList(CwHbParams cwHbParams, Page page, Model model) {
        try {
            fYlist(cwHbParams, page, model);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/cw/cwHb/fYtable", model)).toString();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }


    /**
     * 查询列表
     */
    @RequestMapping("/fYMxlist")
    public String fYMxlist(CwHbParams cwHbParams, Page page, Model model) {
        page.setPageSize(100);
        Table table = cwHbService.queryCwHbTableFyMx(cwHbParams, page);
        model.addAttribute("cwHbsTable", table);
        model.addAttribute("cwHbParams", cwHbParams);
        return "/cw/cwHb/fYlist";
    }
    /**
     * 跳转到选择产品权益页
     */
    @RequestMapping("/doFyMx")
    @ResponseBody
    public String doFyMx(CwHbParams cwHbParams, Page page, Model model) {

        try {
            fYMxlist(cwHbParams, page, model);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/cw/cwHb/fYselectList", model)).toString();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /*
         * 异步查询列表
         */
    @RequestMapping("/fYajaxSelectList")
    @ResponseBody
    public String fYajaxSelectList(CwHbParams cwHbParams, Model model, Page page) {
        try {
            fYMxlist(cwHbParams, page, model);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/cw/cwHb/fYselectTable", model)).toString();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }



    /**
     * 查询列表
     */
    @RequestMapping("/mYlist")
    public String mYlist(CwHbParams cwHbParams, Page page, Model model) {
        page.setPageSize(100);
        Table table = cwHbService.queryCwHbTableMy(cwHbParams, page);
        model.addAttribute("cwHbsTable", table);
        model.addAttribute("cwHbParams", cwHbParams);
        return "/cw/cwHb/mYlist";
    }

    /**
     * 异步查询列表
     */
    @RequestMapping("/mYajaxList")
    @ResponseBody
    public String mYajaxList(CwHbParams cwHbParams, Page page, Model model) {
        try {
            mYlist(cwHbParams, page, model);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/cw/cwHb/mYtable", model)).toString();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }


    /**
     * 查询列表
     */
    @RequestMapping("/mYMxlist")
    public String mYMxlist(CwHbParams cwHbParams, Page page, Model model) {
        page.setPageSize(100);
        Table table = cwHbService.queryCwHbTableMyMx(cwHbParams, page);
        model.addAttribute("cwHbsTable", table);
        model.addAttribute("cwHbParams", cwHbParams);
        return "/cw/cwHb/mYlist";
    }
    /**
     * 跳转到选择产品权益页
     */
    @RequestMapping("/doMyMx")
    @ResponseBody
    public String doMyMx(CwHbParams cwHbParams, Page page, Model model) {

        try {
            mYMxlist(cwHbParams, page, model);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/cw/cwHb/mYselectList", model)).toString();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /*
         * 异步查询列表
         */
    @RequestMapping("/mYajaxSelectList")
    @ResponseBody
    public String mYajaxSelectList(CwHbParams cwHbParams, Model model, Page page) {
        try {
            mYMxlist(cwHbParams, page, model);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/cw/cwHb/mYselectTable", model)).toString();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

























    /**
     * 跳转到新增页
     */
    @RequestMapping("/goAdd")
    @ResponseBody
    public String goAdd(Model model) {
        try {
            CwHb cwHb = new CwHb();
            model.addAttribute("cwHb", cwHb);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/cw/cwHb/form", model)).toString();

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     * 跳转到编辑页
     */
    @RequestMapping("/goEdit")
    @ResponseBody
    public String goEdit(Integer id, Model model) {

        try {
            CwHb cwHb = cwHbService.getCwHb(id);
            model.addAttribute("cwHb", cwHb);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/cw/cwHb/form", model)).toString();

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     * 根据IDs删除数据
     */
    @RequestMapping("/delete")
    @ResponseBody
    public String deleteCwHb(String ids) {
        try {
            List<String> idList = new ArrayList<String>();
            if (ids.length() > 0) {
                idList = Arrays.asList(ids.split(","));
            }
            cwHbService.deleteCwHbBatch(idList);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("数据" + ids + "删除异常！");
            return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, SysContant.DELETE_ERROR_MESSAGE).toString();
        }
        return new JsonResponse(JsonResponse.RETURN_SUCCESS, SysContant.DELETE_SUCCESS_MESSAGE).toString();
    }

    /**
     * 保存数据
     */
    @RequestMapping("/save")
    @ResponseBody
    public String saveCwHb(@Valid CwHb cwHb, BindingResult result) {

        try {
            if (result.hasErrors()) {
                List<ObjectError> errors = result.getAllErrors();
                return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, errors.get(0).getDefaultMessage()).toString();
            }
            cwHbService.saveCwHb(cwHb);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("数据CwHb保存异常！");
            return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, SysContant.SAVE_ERROR_MESSAGE).toString();
        }
        return new JsonResponse<String>(JsonResponse.RETURN_SUCCESS, SysContant.SAVE_SUCCESS_MESSAGE).toString();
    }



}
